﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BUS
{
    public class CDeleteCode
    {

        public static void DeleteCode(ref RichTextBox richTextBox)
        {
                int _Start = richTextBox.SelectionStart;
                int _Length = richTextBox.SelectionLength;
                if (_Start > 0)
                {
                    if (richTextBox.Text[_Start - 1] == '(' || richTextBox.Text[_Start - 1] == '<')
                        richTextBox.SelectionStart--;
                    else
                    {
                        int i = _Start;
                        if (_Length == 0)
                            i--;
                        for (i--; i >= 0; i--)
                        {
                            if (richTextBox.Text[i] == ')' || richTextBox.Text[i] == '>')
                                break;
                            if (richTextBox.Text[i] == '(' || richTextBox.Text[i] == '<')
                            {
                                richTextBox.SelectionStart = i;
                                richTextBox.SelectionLength += _Start - i;
                                break;
                            }
                        }
                    }
                    for (int i = _Start + _Length; i < richTextBox.Text.Length; i++)
                    {
                        if (richTextBox.Text[i] == '(' || richTextBox.Text[i] == '<')
                            break;
                        if (richTextBox.Text[i] == ')' || richTextBox.Text[i] == '>')
                        {
                            richTextBox.SelectionLength = i - richTextBox.SelectionStart + 1;
                        }
                    }

                    if (richTextBox.SelectionLength > 0)
                    {
                        int _temp = richTextBox.SelectionStart;
                        richTextBox.Text = richTextBox.Text.Remove(richTextBox.SelectionStart, richTextBox.SelectionLength);
                        richTextBox.SelectionStart = _temp;
                    }
                    else
                    {
                        richTextBox.Text = richTextBox.Text.Remove(_Start - 1, 1);
                        richTextBox.SelectionStart = _Start - 1;
                    }
                }
        }

    }
}
